package com.sc.nft.admin.controller;

import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.ecology.AdminEcologyPointsUserPageDetailsVO;
import com.sc.nft.entity.vo.ecology.AdminEcologyPointsUserPageVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.UserEcologyPointsWalletLogService;
import com.sc.nft.service.UserEcologyPointsWalletService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * @author Radish
 * @Description:
 * @date 2024-12-02
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/ecology/points/wallet")
@Api(tags = "后台用户积分管理")
public class AdminUserEcologyPointsWalletController {



    private final UserEcologyPointsWalletLogService userEcologyPointsWalletLogService;

    private final Redisson redisson;

    private final UserEcologyPointsWalletService userEcologyPointsWalletService;



/*    @PostMapping(value = "/getUserPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页获取用户积分列表", notes = "分页获取用户积分列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminEcologyPointsUserWalletPageVO> getUserPage(@Valid @RequestBody AdminEcologyPointsUserPageRequest request) {
        return PageResult.success(userEcologyPointsWalletService.getUserPage(request.getUserTel(),request.getPageNo(),request.getPageSize()));
    }*/

    @PostMapping(value = "/editUserPoints", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑用户生态积分", notes = "编辑用户生态积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result editUserPoints(@Valid @RequestBody AdminEditUserPointsWalletRequest request) {
        RLock lock = redisson.getLock(RedisKeyEnum.EDIT_USER_POINTS_LOCK.getKey() + request.getId());
        try {
            boolean res = lock.tryLock(3, TimeUnit.SECONDS);
            if (res) {
                userEcologyPointsWalletService.editUserPoints(request.getId(),request.getUserId(),request.getPointsName(),
                        request.getEcologyPointsWalletEnum(),request.getAmount(),request.getRemark());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @PostMapping(value = "/getUserDetailsPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页获取用户积分记录明细列表", notes = "分页获取用户积分记录明细列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminEcologyPointsUserWalletPageDetailsVO> getUserDetailsPage(@Valid @RequestBody AdminEcologyPointsUserPageDetailsRequest request) {
        return PageResult.success(userEcologyPointsWalletLogService.getUserDetailsPage(request.getUserId(),request.getWalletId(),request.getType(),request.getStartTime(),request.getEndTime(),request.getPageNo(),request.getPageSize()));
    }

    @PostMapping(value = "/adminUserPointWalletPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页获取用户积分列表", notes = "分页获取用户积分列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminUserEcologyPointWalletPageVO> adminUserPointWalletPage(@Valid @RequestBody AdminUserEcologyPointWalletRequest request) {
        return PageResult.success(userEcologyPointsWalletService.adminUserPointWalletPage(request.getPageNo(),request.getPageSize(),request.getUserId()));
    }

    @PostMapping(value = "/adminDaoPointWalletPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页获取dao积分列表", notes = "分页获取dao积分列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminDaoEcologyPointWalletPageVO> adminDaoPointWalletPage(@Valid @RequestBody AdminDaoEcologyPointWalletRequest request) {
        return PageResult.success(userEcologyPointsWalletService.adminDaoPointWalletPage(request.getPageNo(),request.getPageSize(),request.getUserCreatorId()));
    }


    @PostMapping(value = "/adminEcologyPointsUserWalletData", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "数据总览 积分", notes = "数据总览 积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AdminEcologyPointsUserWalletDataVO> adminEcologyPointsUserWalletData(@Valid @RequestBody AdminEcologyPointsUserWalletDataRequest request) {
        return Result.success(userEcologyPointsWalletLogService.adminEcologyPointsUserWalletData(request.getPointsId(),request.getStartTime(),request.getEndTime()));
    }


}
